var http = require("http");
var config = require("./config.js");
var url = require("url");
var request = require("request");
var express = require("express");
var cheerio = require("cheerio");
var async = require("async");
var mysql = require("mysql")
var fs = require("fs");

var app = express();
var connection = mysql.createConnection(config.mysqlDb);

app.engine("html",require("express-art-template"));
app.use('/static/',express.static("./static/"));
// app.set("views","./page") //改变默认视图路径

app.get("/",function(req,res){
  res.render("index.html",{data:[]})
})

app.get("/getReptile",function(req,res){
  var query = req.query;
  var arr = [];
  var page = query.page?query.page:1;
  var limit = query.limit?query.limit:1;
  var path = query.path;
  var keyword = encodeURI(query.keyword);  //转成utf-8,不能直接使用中文
  var base_path = url.parse(path).protocol + "//" + url.parse(path).hostname;
  var bf = query.bf;
  var s = limit - page + 1;
  var options = [];
  for(var i=1;i<=s;i++){
    var obj = {
      page,
      keyword,
      path
    }
    options.push(obj)
    page++;
  }
  switch(query.type){
    case "1": //赣企在线模式
      async.mapLimit(options,bf,goRequest01.bind(this),function(err,result){ //result为数组 开启10时，同时并发处理10个goRequest01函数
        if(err){
          console.log("mapLimit_err",err)
          return false;
        }
        for(let o of result){
          arr = arr.concat(dealData01(o))
        }
        res.render('getReptile.html',{data:arr,base_path,type:query.type})
      })
    break;
    case "2":
      async.mapLimit(options,bf,goRequest02.bind(this),function(err,result){ //result为数组 开启10时，同时并发处理10个goRequest01函数
        if(err){
          console.log("mapLimit_err",err)
          return false;
        }
        for(let o of result){
          arr = arr.concat(dealData02(o))
        }
        res.render('getReptile.html',{data:arr,base_path,type:query.type})
      })
    break;
    case "3":
      async.mapLimit(options,bf,goRequest03.bind(this),function(err,result){ //result为数组 开启10时，同时并发处理10个goRequest01函数
        if(err){
          console.log("mapLimit_err",err)
          return false;
        }
        // for(let o of result){
        //   arr = arr.concat(dealData03(o))
        // }
        res.render('getReptile.html',{data:arr,base_path,type:query.type})
      })
    break;
    default:
      res.render('getReptile.html',{data:arr});
    break;
  }
})

app.get("/getReptile02.html",function(req,res){
  var query = req.query;
  var type = query.type;
  var path = query.path;
  var base_path = url.parse(path).protocol + "//" + url.parse(path).hostname;
  var sql = "";
  var arr = [];
  switch(type){
    case "1":
      sql = "SELECT * FROM ganqi"
      connection.query(sql,function(err,result){
        if(err){
          console.log(err)
          return res.render('getReptile02.html',{data:arr,base_path,type})
        }
        for(let i of result){
          i.detail_img = JSON.parse(i.detail_img)
        }
        arr = result;
        return res.render('getReptile02.html',{data:arr,base_path,type})
      })
    break;
    case "2":
      sql = "SELECT * FROM douban"
      connection.query(sql,function(err,result){
        if(err){
          console.log(err)
          return res.render('getReptile02.html',{data:arr,base_path,type})
        }
        arr = result;
        return res.render('getReptile02.html',{data:arr,base_path,type})
      })  
    break;
  }
}) 

function goRequest01(item,callback){
  var rule = item.path+"?page="+item.page+"&keyword="+item.keyword;
  request(rule,function(err,result,body){
    if(err){
      callback(err) 
    }
    callback(null,result)  
  })
}

function goRequest02(item,callback){
  var page = 25*(item.page-1)
  var rule = item.path+"?start="+page;
  request(rule,function(err,result){
    if(err){
      callback(err)
    }
    callback(null,result)
  }) 
}

function goRequest03(item,callback){
  console.log("item",item)
  var page = 44*(item.page-1)
  console.log(page)
  var rule = item.path+"?s="+page+"&q="+item.keyword;
  console.log(rule)
  request({url:rule,headers:{
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"
  }},function(err,result){
    if(err){
      callback(err)
    }
    callback(null,result)
  })
}


function dealData01(re){
  var arr = [];
  var $ = cheerio.load(re.body);
  var sql = "INSERT INTO ganqi (goods_id,store_id,list_img,title,price,store,detail_href,store_href,detail_img) "
  $(".list_pic .item").each(function(i){
    var obj = {};
    obj.goods_id = $(this).children(".goods-content").attr("dstype_goods")
    obj.store_id = $(this).children(".goods-content").attr("dstype_store")
    obj.list_img = $(this).children(".goods-content").children(".goods-pic").children("a").children("img").attr("src")
    obj.title = $(this).children(".goods-content").children(".goods-info").children(".goods-name").children("a").text()
    obj.price = $(this).children(".goods-content").children(".goods-info").children(".goods-price").children(".sale-price").text().replace("¥","")
    obj.store = $(this).children(".goods-content").children(".goods-info").children(".store").children("a").text()
    obj.detail_href = $(this).children(".goods-content").children(".goods-pic").children("a").attr("href")
    obj.store_href = $(this).children(".goods-content").children(".goods-info").children(".store").children("a").attr("href")
    obj.detail_img = []
    $(this).children(".goods-content").children(".goods-info").children(".goods-pic-scroll-show").children("ul").children("li").each(function(j){
      var detail_img_obj = {}
      var that = this;
      var a = $(that).children("a").children("img").attr("src").split("/")
      detail_img_obj.name = a[a.length-1]
      detail_img_obj.src = $(that).children("a").children("img").attr("src")
      obj.detail_img.push(detail_img_obj)
    })
    // console.log(obj,i,JSON.stringify(obj.detail_img))
    if(i<$(".list_pic .item").length-1){
      sql += "select goods_id,store_id,list_img,title,price,store,detail_href,store_href,detail_img from (select "+obj.goods_id+" as goods_id,"+obj.store_id+" as store_id,'"+obj.list_img+"' as list_img,'"+obj.title+"' as title,'"+obj.price+"' as price,'"+obj.store+"' as store,'"+obj.detail_href+"' as detail_href,'"+obj.store_href+"' as store_href,'"+JSON.stringify(obj.detail_img)+"' as detail_img) as t where not exists (select 1 from ganqi where goods_id = t.goods_id) UNION ";
    }else{
      sql += "select goods_id,store_id,list_img,title,price,store,detail_href,store_href,detail_img from (select "+obj.goods_id+" as goods_id,"+obj.store_id+" as store_id,'"+obj.list_img+"' as list_img,'"+obj.title+"' as title,'"+obj.price+"' as price,'"+obj.store+"' as store,'"+obj.detail_href+"' as detail_href,'"+obj.store_href+"' as store_href,'"+JSON.stringify(obj.detail_img)+"' as detail_img) as t where not exists (select 1 from ganqi where goods_id = t.goods_id) ";
    }
    arr.push(obj);
  })
  connection.query(sql,function(err){
    if(err){
      console.log(err)
      return err
    }
  })
  return arr;
}

function dealData02(re){
  var arr = [];
  var $ = cheerio.load(re.body);
  var sql = "INSERT INTO douban (paiming,list_img,href,title,rating_num) "
  $(".grid_view li").each(function(i){
    var obj = {};
    obj.paiming = $(this).children(".item").children(".pic").children("em").text()
    obj.list_img = $(this).children(".item").children(".pic").children("a").children("img").attr("src")
    obj.href = $(this).children(".item").children(".pic").children("a").attr("href")
    obj.title = $(this).children(".item").children(".info").children(".hd").children("a").children(".title").eq(0).text()
    // obj.info = $(this).children(".item").children(".info").children(".bd").children("p").html()
    obj.rating_num = $(this).children(".item").children(".info").children(".bd").children(".star").children(".rating_num").text()
    console.log("obj",obj)
    if(i<$(".grid_view li").length-1){
      sql += "select paiming,list_img,href,title,rating_num from (select "+obj.paiming+" as paiming,'"+obj.list_img+"' as list_img,'"+obj.href+"' as href,'"+obj.title+"' as title,"+obj.rating_num+" as rating_num) as t where not exists (select 1 from douban where paiming = t.paiming) UNION ";
    }else{
      sql += "select paiming,list_img,href,title,rating_num from (select "+obj.paiming+" as paiming,'"+obj.list_img+"' as list_img,'"+obj.href+"' as href,'"+obj.title+"' as title,"+obj.rating_num+" as rating_num) as t where not exists (select 1 from douban where paiming = t.paiming) ";
    }
    arr.push(obj)
  })
  connection.query(sql,function(err){
    if(err){
      console.log(err)
      return err
    }
  })
  return arr;
}

// sql
// 5.6版本及以下
// insert into ganqi (title)
// select title from (select '李四' as title) as t
// where not exists (select 1 from ganqi where title = t.title)

app.listen(config.port,function(){
  console.log("gogogo")
})